package com.lh.jianzhioffer;

/**
 * @author: linghao
 * @since: 2023/2/12
 *
 * 请完成一个函数，输入一个二叉树，该函数输出它的镜像
 */
public class 二叉树的镜像27 {

    //dfs 递归交换所有叶子节点的left，right
    public TreeNode mirrorTree(TreeNode root) {
        if(root == null)return null; //直到递归到最后的叶子节点时再递归就返回空
        TreeNode left = mirrorTree(root.left);
        TreeNode right = mirrorTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }
}
